package TestSparkConversionOperator.KeyValuePairRDD;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;

import java.util.Arrays;
import java.util.List;

public class SparkZip {
    public static void main(String[] args) {
        /**
         *zip可以将两个元素数量相同、分区数相同的RDD组合成一个键值对RDD。
         * 如果两个RDD数量、分区数量不同，
         * 进行zip操作会报错。
         */
        SparkConf sparkConf = new SparkConf().setAppName("zip").setMaster("local");
        JavaSparkContext sc = new JavaSparkContext(sparkConf);

        List<String> keys = Arrays.asList("key1", "key2");
        List<String> values = Arrays.asList("values1", "values2");

        JavaRDD<String> keysRdd = sc.parallelize(keys);
        JavaRDD<String> valuesRdd = sc.parallelize(values);

        JavaPairRDD<String, String> zip = keysRdd.zip(valuesRdd);

        System.out.println(zip.collect());



    }
}
